provider "alicloud" {
  profile = "default"
  version = "1.151.0"
}

variable "creation" {
  default = "Rds"
}

variable "name" {
  default = "dbInstancevpc"
}

data "alicloud_zones" "default" {
  available_resource_creation = var.creation
}

resource "alicloud_vpc" "default" {
  vpc_name       = var.name
  cidr_block = "172.16.0.0/16"
}

resource "alicloud_vswitch" "default" {
  vpc_id       = alicloud_vpc.default.id
  cidr_block   = "172.16.0.0/24"
  zone_id      = data.alicloud_zones.default.zones[0].id
  vswitch_name = var.name
}

locals {
  dms_group = ["0.0.0.0/0"]
  default_group = ["172.16.0.0/16"]

  #################################
  #                               #
  #            databases          #
  #                               #
  #################################

  parameters = [
    {
      name = "sql_mode"
      value = ""
    },
    {
      name = "slow_query_log"
      value = "OFF"
    }
  ]

  accounts = [
    {
      name         = "user1"
      password     = "1234abc"
      type         = "Normal"
      privilege    = "ReadWrite"
      databases    = ["dbuserv1", "dbuserv2", "pipeline"]
    },
    {
      name         = "user2"
      password     = "1234abc"
      type         = "Normal"
      privilege    = "ReadWrite"
      databases    = ["dbuserv1"]
    }
  ]

  databases = [
    {
      name          = "dbuserv1"
      character_set = "utf8"
      description   = "db1"
    },
    {
      name          = "dbuserv2"
      character_set = "utf8"
      description   = "db2"
    },
    {
      name          = "pipeline"
      character_set = "utf8"
      description   = "pipeline"
    }
  ]

  ###############
  #backup_policy#
  ###############
  preferred_backup_period     = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
  preferred_backup_time       = "00:00Z-01:00Z"
  backup_retention_period     = 7
  log_backup_retention_period = 7
  enable_backup_log           = true

  tags = {
    app = "ebike"
    tenant = "bolan"
    env    = "prod"
  }

}

resource "alicloud_db_instance" "default" {
  engine               = "MySQL"
  engine_version       = "8.0"
  instance_type        = "rds.mysql.s2.large"
  instance_storage     = "20"
  instance_charge_type = "Postpaid"
  instance_name        = var.name
  vswitch_id           = alicloud_vswitch.default.id
  security_ips         = concat(local.default_group, local.dms_group)
  tags                 = local.tags

  dynamic "parameters" {
    for_each = local.parameters
    content {
      name   = lookup(parameters.value, "name", local.parameters)
      value  = lookup(parameters.value, "value", local.parameters)
    }
  }

}

resource "alicloud_db_connection" "foo" {
  instance_id = alicloud_db_instance.default.id
}

output "alicloud_db_connection" {
  value = alicloud_db_connection.foo.connection_string
}

resource "alicloud_db_database" "default" {
  count         = length(local.databases)
  instance_id   = alicloud_db_instance.default.id
  name          = lookup(local.databases[count.index], "name")
  character_set = lookup(local.databases[count.index], "character_set")
  description   = lookup(local.databases[count.index], "description")
}

resource "alicloud_db_account" "account" {
  count = length(local.accounts)
  db_instance_id = alicloud_db_instance.default.id
  account_name        = lookup(local.accounts[count.index], "name")
  account_password    = lookup(local.accounts[count.index], "password")
  account_type        = lookup(local.accounts[count.index], "type")
}

output "alicloud_db_account_privilege" {
  value = alicloud_db_database.default.*.name
}

resource "alicloud_db_account_privilege" "privilege" {
  count = length(local.accounts)
  instance_id  = alicloud_db_instance.default.id
  account_name = lookup(local.accounts[count.index], "name")
  privilege    = lookup(local.accounts[count.index], "privilege")
  db_names     = lookup(local.accounts[count.index], "databases")
  depends_on = [alicloud_db_account.account, alicloud_db_database.default]
}

resource "alicloud_db_backup_policy" "policy" {
  instance_id = alicloud_db_instance.default.id
  preferred_backup_period = local.preferred_backup_period
  preferred_backup_time   = local.preferred_backup_time
  backup_retention_period = local.backup_retention_period
  log_backup_retention_period = local.log_backup_retention_period
  enable_backup_log = local.enable_backup_log
}

resource "null_resource" "setup_db" {
  depends_on = [alicloud_db_instance.default, alicloud_db_account_privilege.privilege]
  provisioner "local-exec" {
    command = <<EOF
        mysql -u ${lookup(local.accounts[0], "name")} -p${lookup(local.accounts[0], "password")} -h ${alicloud_db_connection.foo.connection_string} pipeline < pipeline.sql
    EOF
  }
}

//resource "alicloud_db_readonly_instance" "default" {
//  master_db_instance_id = alicloud_db_instance.default.id
//  zone_id               = alicloud_db_instance.default.zone_id
//  engine_version        = alicloud_db_instance.default.engine_version
//  instance_type         = alicloud_db_instance.default.instance_type
//  instance_storage      = "30"
//  instance_name         = "${var.name}ro"
//  vswitch_id            = alicloud_vswitch.default.id
//}